package simax.integrator.test

import simax.integrator.model.*
import groovy.time.TimeCategory
import java.text.SimpleDateFormat

class RandomMsgGenerator {
	
	/**
	 * �׽�Ʈ�� ���� Command ��
	 * 
	 * @author YongKi
	 *
	 */
	class TestCommand {
		String req
		String rep
		String from
		String to
		/**
		 * ��������, Chaining �޼��� ��� �ݵ�� ���� �;� �Ǵ� �޼����� ���� ���.
		 */
		TestCommand after
	}
	
	final cmdBestwork          = new TestCommand(req:'BESTWORK_REQ', rep:'BESTWORK_REP', from:'WORKmgr', to:'ADSsrv')
	final cmdToolCondtion      = new TestCommand(req:'TOOL_CONDITION_REQUEST', rep:'TOOL_CONDITION_REPLY', from:'WORKmgr', to:'TOOLmgr')
	final cmdWorkOrderInform   = new TestCommand(req:'WORK_ORDER_INFORM', from:'WORKmgr', to:'EES')
	final cmdWorkOrderInform2  = new TestCommand(after:cmdWorkOrderInform, req:'WORK_ORDER_INFORM', from:'EES', to:'TC')

	final List<TestCommand> commands = [
		cmdBestwork, cmdToolCondtion, cmdWorkOrderInform, cmdWorkOrderInform2
	]

	/** 
	 * Random �ϰ� �ð� �������� ��� �޼����� Generate �Ѵ�.
	 * 
	 * @return
	 */
	List<RvMessage> generate(String lineId, String seg, Date startTime, List<String> eqpIds, List<String> lotIds) {
		
		List<RvMessage> messages = []
		Date time = startTime
		def lotEqpPairs = new UniquePairGenerator().pairs([:], [eqpId:eqpIds, lotId:lotIds])
		
		commands.each { cmd ->
			// Build request message
			assert cmd.req != null

			def cmdPairs = [[cmd.req, cmd.from, cmd.to]]			
			if (cmd.rep != null)
				cmdPairs << [cmd.rep, cmd.to, cmd.from]
								
			cmdPairs.each { cmdName, from, to ->

				lotEqpPairs.each {
					def lotId   = it.lotId
					def eqpId   = it.eqpId 
					def subject = "${lineId}.${seg}.${to}"
					def header  = "${seg}.${to},${seg}.${from},DUMMY"
					def msgbody = "EQPID=${eqpId} LOTID=${lotId}" 
					def command = cmdName
					use (TimeCategory) {
						time = time + 1.seconds
					}
					messages << new RvMessage(time:time, subject:subject, command:command, header:header, body:msgbody)
				}
			}	 
		}
		
		println messages.join("\n")
		return messages
	}	
}